<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Making a Connection</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongo.tutorial.html">« Tutorial</a></li>
      <li style="float: right;"><a href="mongo.tutorial.selectdb.html">Getting a Database »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongo.tutorial.html">Tutorial</a></li>
    <li>Making a Connection</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongo.tutorial.connecting" class="section">
  <h2 class="title">Making a Connection</h2>
  <p class="para">
   To connect to the database server, use one of the following:
  </p>

  <div class="example" id="mongo.tutorial.connecting-example">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;connects&nbsp;to&nbsp;localhost:27017<br /></span><span style="color: #0000BB">$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"mongodb://example.com"&nbsp;</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;connect&nbsp;to&nbsp;a&nbsp;remote&nbsp;host&nbsp;(default&nbsp;port:&nbsp;27017)<br /></span><span style="color: #0000BB">$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"mongodb://example.com:65432"&nbsp;</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;connect&nbsp;to&nbsp;a&nbsp;remote&nbsp;host&nbsp;at&nbsp;a&nbsp;given&nbsp;port<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <p class="para">
   You do not have to explicitly disconnect from the database. The driver uses
   persistent connections and will re-use already established connections.
  </p>
  <div class="section" id="mongo.tutorial.connecting.seealso">
   <h2 class="title">See Also</h2>
   <p class="para">
    The chapter on <a href="mongo.connecting.html" class="link">connecting</a>
    covers different types of connections.
   </p>
   <p class="para">
    The API documentation on the <a href="class.mongoclient.html" class="classname">MongoClient</a> class and
    <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span> give a comprehensive look at
    all possible options with a number of examples.
   </p>
  </div>
 </div></div></div></body></html>